﻿@page "/System/RoleMenu"
@attribute [TabItemOption(Text = "角色菜单")]
@using System.Diagnostics.CodeAnalysis;
@using BootstrapBlazorApp.Shared.Data.SYS;
@inject MessageService msg

<Table TItem="RoleMenuDto" PageItemsSource="@PageItemsSource" AutoGenerateColumns="true"
       IsPagination="true" IsStriped="true" IsBordered="true" IsMultipleSelect="true"
       ShowToolbar="true" ShowExtendButtons="true" ShowLoading="true" ShowSearch="true"
       OnAddAsync="@OnAddAsync" OnEditAsync="@OnEditAsync" OnSaveAsync="@OnSaveAsync" OnDeleteAsync="@OnDeleteAsync"
       OnQueryAsync="@OnQueryAsync" ShowExportButton="true">
    <EditTemplate>
        <Row ItemsPerRow="ItemsPerRow.Two" RowType="RowType.Inline">
            <BootstrapInput TValue="string"
                            @bind-Value="@context.RoleCode"
                            IsAutoFocus="true"
                            ShowLabel="true"
                            DisplayText="角色" />
        </Row>

        <BootstrapBlazorApp.Shared.Pages.Select.MenuSelect SelectedItems="SelectedItems" />
    </EditTemplate>
</Table>

@code {
    /// <summary>
    /// 当前Token信息
    /// </summary>
    [CascadingParameter]
    private ITokenContext? Context { get; set; }

    private List<MenuDto>? SelectedItems { get; set; } = new List<MenuDto>();

    private static IEnumerable<int> PageItemsSource => new int[] { 4, 10, 20 };

    /// <summary>
    /// OnInitialized 方法
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();
    }

    private static async Task<RoleMenuDto> OnAddAsync()
    {
        // 模拟延时

        return new RoleMenuDto() { };
    }

    private async Task<RoleMenuDto> OnEditAsync(RoleMenuDto item)
    {
        SelectedItems =  await Global.ExecuteApiAsync<List<MenuDto>>(() => Request.System.QueryMenusByRoleCodeAsync(item.RoleCode));
        // 模拟延时
        return item;
    }

    private async Task<bool> OnSaveAsync(RoleMenuDto item, ItemChangedType changedType)
    {
        if(SelectedItems.Count <= 0)
        {
            await msg.Show(new MessageOption()
                 {
                     Color = Color.Danger,
                     Content = "请选择至少一条菜单！"
                 });
            return false;
        }

        item.MenuCodes = SelectedItems.Select(o => o.MenuCode).ToList();
        if (changedType == ItemChangedType.Add)
        {
            item.Creater = Context.CurrentUser.Account;
            item.CreatedName = Context.CurrentUser.UserName;
        
            return await Global.ExecuteApiAsync(() => Request.System.AddRoleMenuAsync(item));
        }
        else
        {
            item.Modifier = Context.CurrentUser.UserName;
            item.ModifiedName = Context.CurrentUser.UserName;
            return await Global.ExecuteApiAsync(() => Request.System.UpdateRoleMenuAsync(item));
        }
    }

    private async Task<bool> OnDeleteAsync(IEnumerable<RoleMenuDto> items)
    {
        return await Global.ExecuteApiAsync(() => Request.System.DeleteMutiRoleMenuAsync(items.ToList()));
    }

    private async Task<QueryData<RoleMenuDto>> OnQueryAsync(QueryPageOptions options)
    {
        try
        {
            var model = await Request.System.QueryRoleMenuAsync(QueryParameter.GetQueryParameter(options));
            return model.data;
        }
        catch (Exception ex)
        {
            return new QueryData<RoleMenuDto>();
        }


    }

}
